package com.bw.wxy.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.bw.wxy.R;
import com.umeng.commonsdk.debug.W;

public class BottomBut extends RelativeLayout implements View.OnClickListener {
    private int normalImg = 0; //未选中的图片
    private int selImg = 0;  //选中的图片
    private int normalColor = 0;  //未选中的字体颜色
    private int selColor = 0;  //选中的字体颜色
    private String content; //底部文字
    private boolean showPoint = false; //是否显示红点
    private boolean selFlag = false; //是否选中

    private LinearLayout linearLayout;
    private ImageView img;
    private TextView contentTv;
    private TextView numTv;
    private SelectListener selectListener;

    public BottomBut setSelectListener(SelectListener selectListener) {
        this.selectListener = selectListener;
        return this;
    }

    public BottomBut setNormalImg(int normalImg) {
        this.normalImg = normalImg;
        if (!selFlag) {
            img.setImageResource(this.normalImg);
        }
        return this;
    }

    public BottomBut setSelImg(int selImg) {
        this.selImg = selImg;
        if (selFlag) {
            img.setImageResource(this.selImg);
        }
        return this;
    }

    public BottomBut setNormalColor(int normalColor) {
        this.normalColor = normalColor;
        if (!selFlag) {
            contentTv.setTextColor(this.normalColor);
        }
        return this;
    }

    public BottomBut setSelColor(int selColor) {
        this.selColor = selColor;
        if (selFlag) {
            contentTv.setTextColor(this.selColor);
        }
        return this;
    }

    public BottomBut setContent(String content) {
        this.content = content;
        if (this.content.isEmpty()) {
            return this;
        }
        contentTv.setText(content);
        return this;
    }

    public BottomBut setShowPoint(boolean showPoint) {
        this.showPoint = showPoint;
        if (this.showPoint) {
            numTv.setVisibility(VISIBLE);
        } else {
            numTv.setVisibility(GONE);
        }
        return this;
    }

//    public BottomBut setnumTv(String content) {
//        this.content = content;
//        if (this.content.isEmpty()) {
//            return this;
//        }
//        contentTv.setText(content);
//        return this;
//    }


    public BottomBut(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.bottom_arrts);
        selFlag=typedArray.getBoolean(R.styleable.bottom_arrts_selFlag,false);
        typedArray.recycle(); //释放对象
        init();
    }

//    private boolean isShow;
//    public BottomBut isShow(boolean isShow){
//        this.isShow = isShow;
//        init();
//
//        return this;
//    }


    private void init() {
        linearLayout=new LinearLayout(getContext());
        img=new ImageView(getContext());
        contentTv=new TextView(getContext());
        numTv=new TextView(getContext());
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams imgLp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        img.setLayoutParams(imgLp);
        LinearLayout.LayoutParams contentLp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        contentTv.setLayoutParams(contentLp);
        linearLayout.setGravity(Gravity.CENTER);
        linearLayout.addView(img);
        linearLayout.addView(contentTv);
        RelativeLayout.LayoutParams linearLp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLp.addRule(RelativeLayout.CENTER_IN_PARENT);
        linearLayout.setLayoutParams(linearLp);
        addView(linearLayout);
        RelativeLayout.LayoutParams numLp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        numLp.rightMargin= ConvertUtils.dp2px(15);
        numLp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        numTv.setLayoutParams(numLp);
        numTv.setText("1");
        addView(numTv);
        setOnClickListener(this::onClick);
//        if(isShow){
//            CircleView circleView = new CircleView(getContext());
//            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            layoutParams.setMargins(120,20,0,0);
//            circleView.setLayoutParams(layoutParams);
//            addView(circleView);
//        }

    }


    @Override
    public void onClick(View v) {
        selFlag=true;
        refreshSelect();
        if (selectListener!=null){
            selectListener.onSelect(getId());
        }
    }

    public void clearSelect(){
        selFlag=false;
        img.setImageResource(normalImg);
        contentTv.setTextColor(normalColor);
    }

    public void refreshSelect(){
        if (selFlag){
            img.setImageResource(selImg);
            contentTv.setTextColor(selColor);
            numTv.setVisibility(GONE);
        }
    }

    public interface SelectListener{
        void onSelect(int id);
    }

}

